package com.heima.article.listener;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

/**
 * @author zsk
 * @create 2022-04-16-17:48
 */
@Service
public class UpDownListener {

    @Autowired
    private IApArticleService articleService;

    //监听消息
    @KafkaListener(topics = {"${topic.upDownTopic}"})
    public void handleMessage(ConsumerRecord<String,String> record){

        //接受值
        String key = record.key();
        String value = record.value();

        //更新文章的状态，是否上下架
        LambdaUpdateWrapper<ApArticle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ApArticle::getId,key);
        //与文章表中正好相反
        wrapper.set(ApArticle::getIsDown,value.equals("true") ? false:true);
        //修改
        articleService.update(wrapper);
    }


}
